﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using FileManager_Client.PresentationLayer.Common;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class FileManager_Form
    {
        #region Upload Log Shown Event
        private static List<UploadLogView> UploadLogCollection = new List<UploadLogView>();
        void FileManager_Form_Shown_UploadLog(object sender, EventArgs e)
        {
            var logCollection = UserLocalFile.GetFileUploadLogObject();
            if (logCollection.Count <= 0) return;
            UploadLogCollection.AddRange(logCollection);
            Olv_UploadLog.SetObjects(logCollection);
        }
        #endregion

        #region Add Upload Log Method
        private void AddUploadLog(UploadFileProgress uploadFileProgress)
        {
            var addUploadLog = new UploadLogView
                                   {
                                       BeginTime = uploadFileProgress.BeginTime,
                                       DisplaySize = uploadFileProgress.DisplaySize,
                                       EndTime = DateTime.Now,
                                       FileGuid = uploadFileProgress.FileGuid,
                                       FileLength = uploadFileProgress.FileSize,
                                       FileName = uploadFileProgress.FileName,
                                       FolderId = uploadFileProgress.FolderId,
                                       LocalPath = uploadFileProgress.FileLocalPath,
                                       ServerPath = uploadFileProgress.ServerPath
                                   };
            UserLocalFile.SaveFileUploadLogObject(addUploadLog);
            UploadLogCollection.Add(addUploadLog);
            Olv_UploadLog.AddObject(addUploadLog);
            Olv_UploadLog.RefreshObject(addUploadLog);
        }
        #endregion

        #region UploadLog ContextMenu Event
        private void cms_UploadLog_Opening(object sender, CancelEventArgs e)
        {
            Tsmi_OpenUploadFolder.Enabled = null != Olv_UploadLog.SelectedItem;
            Tsmi_OpenUploadLocalFolder.Enabled = null != Olv_UploadLog.SelectedItem;
            Tsmi_DeleteUploadLog.Enabled = null != Olv_UploadLog.SelectedItem;
            Tsmi_DeleteAllUploadLog.Enabled = Olv_UploadLog.Items.Count > 0;
        }

        private void Tsmi_DeleteUploadLog_Click(object sender, EventArgs e)
        {
            if(null == Olv_UploadLog.SelectedObject)return;
            var logObject = Olv_UploadLog.SelectedObject as UploadLogView;
            if (null == logObject) return;
            Olv_UploadLog.RemoveObject(logObject);
            UploadLogCollection.Remove(logObject);
            UserLocalFile.RemoveFileUploadLogObject(logObject.FileGuid);
        }

        private void Tsmi_DeleteAllUploadLog_Click(object sender, EventArgs e)
        {
            var delObj = new List<UploadLogView>();
            foreach (UploadLogView del in Olv_UploadLog.Objects)
            {
                UserLocalFile.RemoveFileUploadLogObject(del.FileGuid);
                delObj.Add(del);
                UploadLogCollection.Remove(del);
            }
            foreach (var uploadFileProgress in delObj)
                Olv_Uploading.RemoveObject(uploadFileProgress);
        }

        private void Tsmi_OpenUploadFolder_Click(object sender, EventArgs e)
        {
            if (null == Olv_UploadLog.SelectedObject) return;
            var logObject = Olv_UploadLog.SelectedObject as UploadLogView;
            if(null == logObject) return;
            ToolBar_System.SelectedIndex = ToolBarItemIndex.FileManager;
            UpdateListView(logObject.FolderId);
        }

        private void Tsmi_OpenUploadLocalFolder_Click(object sender, EventArgs e)
        {
            if (null == Olv_UploadLog.SelectedObject) return;
            var logObject = Olv_UploadLog.SelectedObject as UploadLogView;
            if (null == logObject) return;
            if(string.IsNullOrEmpty(logObject.LocalPath))return;
            Process.Start("Explorer.exe", "/select," + logObject.LocalPath);
        }
        #endregion
    }

    #region Upload Log Object
    [Serializable]
    public class UploadLogView
    {
        public string FileName { get; set; }
        public DateTime BeginTime { get; set; }
        public string LocalPath { get; set; }
        public double DisplaySize { get; set; }
        public string FileGuid { get; set; }
        public string FolderId { get; set; }
        public long FileLength { get; set; }
        public string ServerPath { get; set; }
        public DateTime EndTime { get; set; }
    }
#endregion
}